using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScrollWheel : MonoBehaviour
{
	public delegate void SetItemLookDelegate(GameObject item, int index);

	public GameObject scrollItemPrefab;

	public UIPanel container;

	public GameObject table;

	public float lowAlpha = 0.25f;

	public float lowScale = 0.9f;

	public List<GameObject> items = new List<GameObject>();

	protected SetItemLookDelegate lookDelegate;

	private float widgetHeight;

	private float containerHeight;

	private float containerMiddleY;

	protected Vector3 tableZeroPos;

	public int index { get; protected set; }

	public void Init(SetItemLookDelegate lookDelegate, int startIndex)
	{
		this.lookDelegate = lookDelegate;
		index = startIndex;
		if (container == null)
		{
			container = base.gameObject.GetComponent<UIPanel>();
		}
		UIWidget component = scrollItemPrefab.GetComponent<UIWidget>();
		Vector3[] worldCorners = component.worldCorners;
		widgetHeight = Mathf.Abs(worldCorners[0].y - worldCorners[1].y);
		Vector3[] worldCorners2 = container.worldCorners;
		containerHeight = Mathf.Abs(worldCorners2[0].y - worldCorners2[1].y);
		int num = Mathf.Max(0, Mathf.CeilToInt((containerHeight - widgetHeight) * 0.5f / widgetHeight)) * 2 + 1;
		items.Add(scrollItemPrefab);
		containerMiddleY = (worldCorners2[0].y + worldCorners2[1].y) * 0.5f;
		float num2 = containerMiddleY - widgetHeight * (float)(num - 1) * 0.5f;
		tableZeroPos = table.transform.position;
		for (int i = 0; i < num; i++)
		{
			GameObject gameObject = null;
			gameObject = ((i != 0) ? NGUITools.AddChild(table, scrollItemPrefab) : scrollItemPrefab);
			gameObject.SetActive(true);
			items.Add(gameObject);
			Transform transform = gameObject.transform;
			Vector3 position = transform.position;
			position.y = num2;
			transform.position = position;
			num2 += widgetHeight;
		}
		UpdateItemLooks(index);
		AdjustItemsForDistance();
	}

	public void UpdateItemLooks(int index)
	{
		index -= items.Count / 2;
		foreach (GameObject item in items)
		{
			lookDelegate(item, index);
			index++;
		}
	}

	public void StartSpin(float duration, int repeatTimes, Action onComplete)
	{
		StopAllCoroutines();
		StartCoroutine(DoStartSpin(duration, repeatTimes, onComplete));
	}

	public void SlowDownSpin(float fromDuration, float toDuration, int times, Action onComplete)
	{
		StopAllCoroutines();
		StartCoroutine(DoSlowSpin(fromDuration, toDuration, times, onComplete));
	}

	private IEnumerator DoStartSpin(float duration, int repeatTimes, Action onComplete)
	{
		float time = 0f;
		float timesRepeated = 0f;
		float totalDuration = (float)repeatTimes * duration;
		float lastOffset = 0f;
		while (time <= totalDuration)
		{
			time += Time.deltaTime;
			float s = widgetHeight * time / duration;
			if (s - lastOffset >= widgetHeight)
			{
				index += Mathf.FloorToInt((s - lastOffset) / widgetHeight);
				lastOffset = Mathf.Floor(s / widgetHeight) * widgetHeight;
				UpdateItemLooks(index);
			}
			Vector3 pos = tableZeroPos;
			pos.y += 0f - (s - lastOffset);
			table.transform.position = pos;
			AdjustItemsForDistance();
			yield return null;
		}
		table.transform.position = tableZeroPos;
		if (onComplete != null)
		{
			onComplete();
		}
	}

	private IEnumerator DoSlowSpin(float fromDuration, float toDuration, int times, Action onComplete)
	{
		float time2 = 0f;
		float totalTime = 0f;
		int spinTimes = 0;
		float duration2 = 0f;
		float s2 = (float)(times - 1) * widgetHeight;
		float v = widgetHeight / toDuration;
		float v2 = widgetHeight / fromDuration;
		float totalDuration = 2f * s2 / (v + v2);
		float a = (v - v2) / totalDuration;
		float lastOffset = 0f;
		Debug.Log("Total duration " + totalDuration + " a " + a);
		while (totalTime <= totalDuration)
		{
			totalTime += Time.deltaTime;
			s2 = a * (totalTime * totalTime) * 0.5f + v2 * totalTime;
			if (s2 - lastOffset >= widgetHeight)
			{
				index += Mathf.FloorToInt((s2 - lastOffset) / widgetHeight);
				lastOffset = Mathf.Floor(s2 / widgetHeight) * widgetHeight;
				UpdateItemLooks(index);
			}
			Vector3 pos = tableZeroPos;
			pos.y += 0f - (s2 - lastOffset);
			table.transform.position = pos;
			AdjustItemsForDistance();
			yield return null;
		}
		duration2 = toDuration;
		time2 = 0f;
		while (time2 <= duration2)
		{
			time2 += Time.deltaTime;
			table.transform.position = tableZeroPos + Vector3.Lerp(Vector3.zero, Vector3.down * widgetHeight, time2 / duration2);
			AdjustItemsForDistance();
			yield return null;
		}
		table.transform.position = tableZeroPos;
		index++;
		UpdateItemLooks(index);
		AdjustItemsForDistance();
		if (onComplete != null)
		{
			onComplete();
		}
	}

	private void AdjustItemsForDistance()
	{
		foreach (GameObject item in items)
		{
			UIWidget component = item.GetComponent<UIWidget>();
			float t = 2f * Mathf.Abs(component.cachedTransform.position.y - containerMiddleY) / containerHeight;
			component.alpha = Mathf.Lerp(1f, lowAlpha, t);
			component.cachedTransform.localScale = Vector3.Lerp(Vector3.one, new Vector3(1f, lowScale, 1f), t);
		}
	}
}
